package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.Recharge;
import com.qfedu.openplateform.manager.entity.Res;
import com.qfedu.openplateform.manager.service.RechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lileijian
 * @create 2021-12-02 21:38
 */
@RestController
@RequestMapping("/sys/recharge")
public class RechargeController {

    @Autowired
    private RechargeService rechargeService;


    @RequestMapping("/add")
    public Res addRecharge(Recharge recharge) {
        try {
            rechargeService.addRecharge(recharge);
            return Res.ok(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res.error(false, "添加失败");
    }

    @RequestMapping("/del")
    public Res delRecharge(Integer[] ids) {
        try {
            rechargeService.delRecharges(ids);
            return Res.ok(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res.error(false, "删除失败");
    }

    @RequestMapping( "/update")
    public Res updateRecharge(Recharge recharge) {
        try {
            rechargeService.updateRecharge(recharge);
            return Res.ok(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res.error(false, "修改失败");
    }

    @RequestMapping( "/table")
    public Res table(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Recharge> rechargeList = rechargeService.getRechargeList();
            PageInfo<Recharge> pageInfo = new PageInfo<>(rechargeList);
            return Res.ok(pageInfo.getList(),pageInfo.getTotal());
        } catch (Exception e) {
            Res.exception();
        }
        return Res.error("获取失败，请稍后重试。");
    }

}
